package za.org.jcicada.genss.sensor.impl;

import java.util.HashMap;
import java.util.Map;

import za.org.jcicada.genss.sensor.ISensorType;
import za.org.jcicada.genss.sensor.IStatusStrategy;
import za.org.jcicada.genss.sensor.SensorStatus;

/**
 * Records the mapping for a discrete sensor of value to status.
 * @author rcrida
 */
public class StatusStrategyDiscrete implements IStatusStrategy {

	/** The map from value to status */
	private Map<String, SensorStatus> statusMap;
	
	/** The type of discrete sensor this work with */
	private TypeDiscrete type;
	
	/**
	 * Constructor
	 * @param type the type of discrete sensor this works with
	 * @param statusMap the map
	 */
	public StatusStrategyDiscrete(TypeDiscrete type, Map<String, SensorStatus> statusMap) {
		this.type = type;
		this.statusMap = statusMap;
	}
	
	public StatusStrategyDiscrete(TypeDiscrete type, String params) {
		this.type = type;
		statusMap = new HashMap<String, SensorStatus>();
		for (String option : params.split(",")) {
			String[] kv = option.split("=>");
			statusMap.put(kv[0], SensorStatus.valueOf(kv[1]));
		}
		if (!statusMap.keySet().equals(type.getValueSet())) {
			throw new IllegalArgumentException(String.format("Discrete keys: %s don't match those for type: %s", statusMap.keySet().toString(), type.getValueSet().toString()));
		}
	}
	
	public SensorStatus getStatus(Object value) throws InvalidSensorValue {
		return statusMap.get(value);
	}

	public boolean isValidSensorType(ISensorType sensorType) {
		return type.equals(sensorType);
	}

}
